package com.java_foundation.thread_demo.test_11;

/**
 * @program: java_foundation
 * @description: 线程死锁实例
 * @author: xiongbangwen <Email>xiongbangwen@163.com</Email>
 * @create: 2020-07-09 22:52
 **/
public class DeadLockDemo {

    //创建资源
    private static Object resourceA = new Object();
    private static Object resourceB = new Object();

    public static void main(String[] args) {
        //创建线程A
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println(Thread.currentThread() + "获取资源resourceA");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread() + "等待获取资源resoureB");
                    synchronized(resourceB){
                        System.out.println(Thread.currentThread()+"获取资源resourceB");
                    }
                }
            }
        });
        //创建线程B
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceB) {
                    System.out.println(Thread.currentThread() + "获取资源resourceB");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread() + "等待获取资源resoureA");
                    synchronized(resourceA){
                        System.out.println(Thread.currentThread()+"获取资源resourceA");
                    }
                }
            }
        });

        threadA.start();
        threadB.start();
    }

}
